package com.easy.annex.controller;

import com.easy.annex.service.IAnnexService;
import com.platform.base.common.dto.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;

@RestController
@Api(tags = "大附件")
@RequestMapping("/annex")
public class AnnexController {

    @Resource
    IAnnexService annexService;

    @ApiOperation(value = "上传单个大附件")
    @PostMapping("api/v1/big/file")
    public Result<String> uploadFile(@ApiParam(name = "file", value = "附件", required = true) MultipartFile file) {
        return new Result<>(annexService.uploadFile(file));
    }

    @ApiOperation(value = "上传多个大附件")
    @PostMapping("api/v1/big/files")
    public Result<List<String>> uploadFiles(@ApiParam(name = "files", value = "多附件", required = true) MultipartFile[] files) {
        return new Result<>(annexService.uploadFiles(files));
    }

    @ApiOperation(value = "下载单个大附件")
    @GetMapping(value = "api/v1/down/big/file")
    public void downFile(@RequestParam @NotBlank String fileId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        annexService.downFile(fileId, request, response);
    }

    @ApiOperation(value = "删除单个大附件")
    @DeleteMapping(value = "api/v1/big/file")
    public Result<Boolean> deleteFile(@RequestParam @NotBlank String fileId) throws IOException {
        return new Result<>(annexService.delete(fileId));
    }

    @ApiOperation(value = "批量删除大附件")
    @DeleteMapping(value = "api/v1/big/file/batch")
    public @ResponseBody Result<Boolean> batchDeleteFile(@RequestBody List<String> fileIds) throws IOException {
        return new Result<>(annexService.batchDelete(fileIds));
    }
}